package com.cn.onetrip.activity;

import java.util.List;

import com.cn.onetrip.adapter.RouteDetailItemAdapter;
import com.cn.onetrip.objects.ArticleListObj;
import com.cn.onetrip.objects.ArticleObj;
import com.cn.onetrip.objects.VisitsListObj;
import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.SysApplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RouteDetailActivity extends Activity {

	private ListView routeDetailList;
	private RouteDetailItemAdapter adapter;
	private Button quit;

	private ArticleListObj articleListObj;
	private TextView routeDetailTitle;

	private View footImageView;
	private int screenWidth, screenHeight;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (SysApplication.visitsListObj.size() == 0) {
			finish();
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		setContentView(R.layout.activity_route_detail);

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;

		List<VisitsListObj> visitsList = SysApplication.visitsListObj;
		routeDetailTitle = (TextView) findViewById(R.id.routeDetailTitle);
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		int RouteNum = bundle.getInt("RouteNum");
		int itemNum = bundle.getInt("item");
		routeDetailList = (ListView) findViewById(R.id.routeDetailList);
		adapter = new RouteDetailItemAdapter(RouteDetailActivity.this,
				routeDetailList, handler);
		routeDetailList.setAdapter(adapter);
		quit = (Button) findViewById(R.id.quit);
		quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
				overridePendingTransition(R.anim.push_left_translate,
						R.anim.pull_right_translate);
			}
		});

		VisitsListObj visitsListObj = visitsList.get(RouteNum);
		List<ArticleListObj> detailedSites = visitsListObj.detailedsSites;
		articleListObj = detailedSites.get(itemNum);
		routeDetailTitle.setText(getResources().getString(
				R.string.travel_address)
				+ articleListObj.title);
		loadDate();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		adapter.threadQuit();
		adapter.clear();
		super.onDestroy();
	}

	public void loadDate() {
		adapter.clean();
		List<ArticleObj> articleObjList = articleListObj.detailedsArticleList;
		for (int i = 0; i < articleObjList.size(); i++) {
			ArticleObj articleObj = articleObjList.get(i);
			adapter.addRoutes(articleObj.thumb_icon, articleObj.title,
					articleObj.description);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_left_translate,
					R.anim.pull_right_translate);
		}
		return super.onKeyDown(keyCode, event);
	}

	public Handler handler = new Handler() {
		public void dispatchMessage(android.os.Message msg) {
			if (msg.what == 0) {
				footImageView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.alert_view, null);
				Drawable drawable = (Drawable) msg.obj;
				final ImageView alertImage = (ImageView) footImageView
						.findViewById(R.id.alert_image);
				final AlertDialog dialog = new AlertDialog.Builder(
						RouteDetailActivity.this).create();
				alertImage.setImageDrawable(drawable);
				Window window = dialog.getWindow();
				WindowManager.LayoutParams lp = window.getAttributes();
				lp.alpha = 0.9f;
				window.setAttributes(lp);
				dialog.setView(footImageView, 5, 5, 5, 5);
				dialog.show();
				footImageView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						alertImage.destroyDrawingCache();
						footImageView.destroyDrawingCache();
						dialog.cancel();
					}
				});
			}
		};
	};
}
